package edu.fiu.cis.personalize.server.graph.store;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import edu.fiu.cis.personalize.server.graph.Node;

public abstract class NodeCache<E extends Node> implements Iterable<E> {
	private ConcurrentHashMap<Integer, E> idToNode = new ConcurrentHashMap<Integer, E>();
	private ConcurrentHashMap<String, E> stringToNode = new ConcurrentHashMap<String, E>();
	private AtomicInteger counter = new AtomicInteger();

	@SuppressWarnings("unchecked")
	public void add(Node n) {
		idToNode.put(n.getId(), (E) n);
		stringToNode.put(n.getStringKey(), (E) n);
	}

	public E get(int id) {
		return idToNode.get(id);
	}

	public E get(String s) {
		if (s==null){
			return null;
		}
		return stringToNode.get(s);
	}

	protected int nextId() {
		return counter.incrementAndGet();
	}

	public int getCounterValue() {
		return counter.get();
	}

	public void setCounterValue(int value) {
		counter.set(value);
	}

	@Override
	public Iterator<E> iterator() {
		return idToNode.values().iterator();
	}

	public abstract E create(String stringKey);
	
	

}
